package com.weizai.carshop.controller;

import cn.hutool.core.bean.BeanUtil;
import com.weizai.carshop.common.enumeration.AppHttpCodeEnum;
import com.weizai.carshop.common.result.ResponseResult;
import com.weizai.carshop.model.dto.GoodsProductDto;
import com.weizai.carshop.model.entiity.GoodsProduct;
import com.weizai.carshop.service.GoodsProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/v1/admin/goodsProduct")
@RequiredArgsConstructor
public class GoodsProductController {
    @Autowired
    private GoodsProductService goodsProductService;

    /**
     * 获取商品展示数据
     * @return
     */
    @GetMapping
    public ResponseResult getGoodsProduct() {
        List<GoodsProduct> list = goodsProductService.list();
        return ResponseResult.okResult(list);
    }

    /**
     * 设置商品展示数据
     */
    @PostMapping("/setGoodsProduct")
    public  ResponseResult setGoodsProduct(@RequestBody GoodsProductDto goodsProductDto) {
        GoodsProduct goodsProduct = BeanUtil.copyProperties(goodsProductDto, GoodsProduct.class);
        if(goodsProduct.getId() != null){
            // 设置更新时间
            goodsProduct.setUpdateTime(LocalDateTime.now());
        }else {
            goodsProduct.setCreateTime(LocalDateTime.now());
            goodsProduct.setUpdateTime(LocalDateTime.now());
        }
        goodsProductService.saveOrUpdate(goodsProduct);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除商品展示数据
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult delGoodsProduct(@PathVariable Long id) {
        goodsProductService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
